package com.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "ProductServlet", value = "/ProductServlet")
public class ProductServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        //response.setCharacterEncoding("UTF-8");
        //response.setHeader("Content-Type","text/html;charset=utf-8");
        response.setContentType("text/html;charset=UTF-8");
        String src1 =  request.getParameter("src1");
        String name1=  request.getParameter("name1");
        String price =  request.getParameter("price");
        String userid =  request.getParameter("userid");
        System.out.println("点击加入购物车："+src1+"->"+name1+"->"+price+"->"+userid);

        //先查询商品表是否添加过同种商品(用户ID+商品名称验证)
        selectLog selog = new selectLog();
        try {
         int seli =   selog.selectshopp(userid,name1);
            if(seli == 0){
                //未添加过，调用数据库JDBC给商品表插入数据
                insertLog insert = new insertLog();
                int row = insert.insertlogin(userid,src1,name1,price);
                response.getWriter().write("加入成功");
                System.out.println("成功加入购物车数据"+row+"条");
            } else if (seli == 1) {
                //已添加过更新数量+1
                System.out.println("商品数量已经更新");
                response.getWriter().write("加入成功");
            }
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        doGet(request,response);
    }
}
